// dnSpy decompiler from Assembly-CSharp.dll class: GameObjectsPool
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class GameObjectsPool
{
	public static GameObject GetObjectInstance(GameObject prefab, Vector3 position, Quaternion rotation)
	{
		GameObjectsPool.Pool pool = GameObjectsPool.pools[prefab] as GameObjectsPool.Pool;
		if (pool == null)
		{
		//	pool = (GameObjectsPool.pools[prefab] = new GameObjectsPool.Pool());

		}
		if (pool.available.Count > 0)
		{
			GameObject gameObject = pool.available.Pop();
			pool.taken.Add(gameObject);
			if (gameObject.activeSelf)
			{
				UnityEngine.Debug.LogError("Object on 'available' stack in GameObjectsPool is already active! Something must have corrupted it!");
			}
			gameObject.transform.position = position;
			gameObject.transform.rotation = rotation;
			gameObject.transform.localScale = prefab.transform.localScale;
			gameObject.BroadcastMessage("Awake", null, SendMessageOptions.DontRequireReceiver);
			gameObject.SetActive(true);
			gameObject.BroadcastMessage("Start", null, SendMessageOptions.DontRequireReceiver);
			return gameObject;
		}
		GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(prefab, position, rotation);
		pool.taken.Add(gameObject2);
		GameObjectsPool.hostPoolOfObject[gameObject2] = pool;
		return gameObject2;
	}

	public static void FreeInstance(GameObject obj)
	{
		if (!GameObjectsPool.hostPoolOfObject.ContainsKey(obj))
		{
			UnityEngine.Object.Destroy(obj);
		}
		else
		{
			GameObjectsPool.Pool pool = GameObjectsPool.hostPoolOfObject[obj] as GameObjectsPool.Pool;
			pool.taken.Remove(obj);
			pool.available.Push(obj);
			obj.transform.parent = null;
			obj.SetActive(false);
		}
	}

	public static IEnumerator FreeInstanceAfterDelay(GameObject obj, float delaySec)
	{
		yield return new WaitForSeconds(delaySec);
		GameObjectsPool.FreeInstance(obj);
		yield break;
	}

	public static void PreallocateObjectInstances(GameObject prefab, int count, GameObject parent)
	{
		GameObjectsPool.Pool pool = GameObjectsPool.pools[prefab] as GameObjectsPool.Pool;
		if (pool == null)
		{
//			pool = (GameObjectsPool.pools[prefab] = new GameObjectsPool.Pool());
		}
		for (int i = 0; i < count; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(prefab, Vector3.zero, Quaternion.identity);
			gameObject.SetActive(false);
			if (null != parent)
			{
				gameObject.transform.parent = parent.transform;
			}
			pool.available.Push(gameObject);
			GameObjectsPool.hostPoolOfObject[gameObject] = pool;
		}
	}

	public static void Drain()
	{
		IEnumerator enumerator = GameObjectsPool.pools.Values.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				GameObjectsPool.Pool pool = (GameObjectsPool.Pool)obj;
				foreach (GameObject obj2 in pool.taken)
				{
					UnityEngine.Object.Destroy(obj2);
				}
				foreach (GameObject obj3 in pool.available)
				{
					UnityEngine.Object.Destroy(obj3);
				}
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		GameObjectsPool.pools.Clear();
		GameObjectsPool.hostPoolOfObject.Clear();
	}

	private static Hashtable pools = new Hashtable();

	private static Hashtable hostPoolOfObject = new Hashtable();

	private class Pool
	{
		public Stack<GameObject> available = new Stack<GameObject>();

		public HashSet<GameObject> taken = new HashSet<GameObject>();
	}
}
